/**
 * @author yusak
 */
ChaShaKu.WordControlMenu = Ext.extend(Ext.app.Module,{
	id: 'wordControlMenu',
	run: function(){
		this.app.modules.advancedSearchPanel.afterRenderedModules.push(this);
	},
	create: function(){
		var grid = this.app.modules.advancedSearchPanel.grid;
		var ds = grid.getStore();
		var toolbar = grid.getTopToolbar();
		toolbar.add(
			new Ext.Action({
				iconCls: 'add',
				text: 'Add new keyword',
				handler: function(){
					grid.stopEditing();
					var index = ds.getCount();
					ds.loadData({keywords:[{interval:0}]},true);
					grid.startEditing(index, 1);
				}
			}),
			new Ext.Action({
				iconCls: 'remove',
				text: 'Remove selected keyword',
				handler: function(){
					var sm = grid.getSelectionModel();
					var rows = sm.getSelections();
					grid.stopEditing();
					for(i = 0; i < rows.length; i++) {
						ds.remove(ds.getById(rows[i].id));
					};
				}
			})
		);
		btns = this.app.modules.advancedSearchPanel.searchBtns;
		checkProps = ['surface','reading','basetype_surface']
		ds.on({
			load:function(store,records){
				var flag = records.any(function(record){
					var result = false;
					checkProps.each(function(prop){
						data = record.get(prop);
						if(data){
							m = data.strip();
							if(m){
								result = true	
							}							
						}
					});
					return result;
				});
				var flag2 = records.all(function(record){
					var result = true;
					checkProps.each(function(prop){
						data = record.get(prop);
						if(data){
							m = data.strip();
							if(m.match(/\s/)){
								result = false
							}			
						}
					});
					return result;
				});
				if(flag && flag2){
					btns.each(function(btn){
						btn.enable();
					});
				}else{
					btns.each(function(btn){
						btn.disable();
					});
				}
			},
			remove:function(store){
				if(store.getCount() < 1){
					btns.each(function(btn){
						btn.disable();
					});
				}else{
					var records = store.getRange();
					var flag = records.any(function(record){
						var result = false;
						checkProps.each(function(prop){
							data = record.get(prop);
							if(data){
								m = data.strip();
								if(m){
									result = true	
								}							
							}
						});
						return result;
					});
					var flag2 = records.all(function(record){
						var result = true;
						checkProps.each(function(prop){
							data = record.get(prop);
							if(data){
								m = data.strip();
								if(m.match(/\s/)){
									result = false
								}			
							}
						});
						return result;
					});
					if(flag && flag2){
						btns.each(function(btn){
							btn.enable();
						});
					}else{
						btns.each(function(btn){
							btn.disable();
						});
					}
				}
			},
			scope:this
		});
		grid.on({
			afteredit: function(e){
				var store = e.grid.getStore();
				var records = store.getRange();
				if(e.field == 'center_word' && e.value ){
					records.each(function(record,index){
						if(index != e.row){
							record.set('center_word',false);
						}
					});
				}else{
					var flag = records.any(function(record){
						var result = false;
						checkProps.each(function(prop){
							data = record.get(prop);
							if(data){
								m = data.strip();
								if(m){
									result = true	
								}							
							}
						});
						return result;
					});
					var flag2 = records.all(function(record){
						var result = true;
						checkProps.each(function(prop){
							data = record.get(prop);
							if(data){
								m = data.strip();
								if(m.match(/\s/)){
									result = false
								}			
							}
						});
						return result;
					});
					if(flag && flag2){
						btns.each(function(btn){
							btn.enable();
						});
					}else{
						btns.each(function(btn){
							btn.disable();
						});
					}
				}
			},
			scope: this
		});
	}
});
